/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.espian.showcaseview.sample;

import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.ViewTarget;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

/**
 * fragment添加引导层
 */
public class FragmentDemoAbility extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setMainRoute(FirstDemoFragment.class.getName());
        addActionRoute("second", SecondDemoFragment.class.getName());
    }

    /**
     * fragment
     */
    public static class FirstDemoFragment extends AbilitySlice {
        private Button button;
        private ShowcaseView showcaseView;

        @Override
        protected void onStart(Intent intent) {
            super.onStart(intent);
            ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_fragment_layout, null, false);
            setUIContent(componentContainer);
            button = (Button) findComponentById(ResourceTable.Id_fragment_demo_button);
            button.setText("Show toast first");
            button.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    present(new SecondDemoFragment(), new Intent());
                }
            });
            showcaseView = new ShowcaseView.Builder(getAbility(),
                    (ComponentContainer) componentContainer.getComponentParent())
                    .withMaterialShowcase()
                    .setStyle(ResourceTable.Pattern_CustomShowcaseTheme)
                    .setTarget(new ViewTarget(button))
                    .hideOnTouchOutside()
                    .setContentTitle(ResourceTable.String_showcase_fragment_title)
                    .setContentText(ResourceTable.String_showcase_fragment_message)
                    .build();
        }

        @Override
        protected void onStop() {
            super.onStop();
            showcaseView.destory();
        }
    }

    /**
     * fragment
     */
    public static class SecondDemoFragment extends AbilitySlice {
        private Button button;
        private ShowcaseView showcaseView;

        @Override
        protected void onStart(Intent intent) {
            super.onStart(intent);
            ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_fragment_layout, null, false);
            setUIContent(componentContainer);

            button = (Button) findComponentById(ResourceTable.Id_fragment_demo_button);
            button.setText("Show toast second");
            showcaseView = new ShowcaseView.Builder(getAbility(),
                    (ComponentContainer) componentContainer.getComponentParent())
                    .withMaterialShowcase()
                    .setStyle(ResourceTable.Pattern_CustomShowcaseTheme2)
                    .setTarget(new ViewTarget(button))
                    .hideOnTouchOutside()
                    .setContentTitle(ResourceTable.String_showcase_fragment_title_2)
                    .setContentText(ResourceTable.String_showcase_fragment_message_2)
                    .build();

            button.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    present(new FirstDemoFragment(), new Intent());
                }
            });
        }

        @Override
        protected void onStop() {
            super.onStop();
            showcaseView.destory();
        }

    }
}
